package hos.table.simple;

import android.content.Context;
import android.graphics.Color;
import android.text.Spanned;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.text.HtmlCompat;

import hos.table.BaseTableAdapter;
import hos.table.R;
import hos.table.holder.BaseTableSorterHolder;
import hos.table.model.Cell;


public class SimpleCellViewHolder extends BaseTableSorterHolder<Cell> {

    public SimpleCellViewHolder(@NonNull Context context, int layoutId) {
        super(context, layoutId);
    }

    public SimpleCellViewHolder(@NonNull View itemView) {
        super(itemView);
        View dividerRight = itemView.findViewById(R.id.dividerRight);
        View dividerBottom = itemView.findViewById(R.id.dividerBottom);
        dividerRight.setVisibility(View.GONE);
        dividerBottom.setVisibility(View.GONE);
    }


    @Override
    public void onConvert(@NonNull BaseTableAdapter adapter, @NonNull View itemView, @Nullable Cell model, int columnPosition, int rowPosition) {
        if (model == null) {
            return;
        }
        AppCompatTextView value = itemView.findViewById(R.id.value);
        Object content = model.getContent();
        if (content instanceof Spanned) {
            value.setText((Spanned) model.getContent());
        } else if (content instanceof CharSequence) {
            value.setText((CharSequence) model.getContent());
        } else {
            String contains = String.valueOf(content);
            if (contains.contains(">") || contains.contains("<")) {
                value.setText(HtmlCompat.fromHtml(contains, HtmlCompat.FROM_HTML_MODE_LEGACY));
            } else {
                value.setText(String.valueOf(model.getContent()));
            }
        }
        if (rowPosition % 2 == 0) {
            // 偶数 白色
            itemView.setBackgroundColor(Color.WHITE);
        } else {
            itemView.setBackgroundResource(R.color.table_single_bg);
        }
    }

    @Override
    public void setBackgroundColor(int p_nColor) {
//        if (getBindingAdapterPosition() % 2 == 0) {
//            // 偶数 白色
//            root.setBackgroundColor(Color.WHITE);
//        } else {
//            root.setBackgroundResource(R.color.table_single_bg);
//        }
    }
}
